#ifndef SERIALPORT_H
#define SERIALPORT_H

#include <QWidget>
#include <QComboBox>
#include <QFile>

class QTextEdit;
class QSerialPort;
class QPushButton;
class QTimer;
class QSpinBox;

class SerialPort : public QWidget
{
    Q_OBJECT
public:
    explicit SerialPort(QWidget *parent = nullptr);
    ~SerialPort();

signals:
    void signalUpdateTitle(QString text);
    void signalOpenSerial(void);
    void signalCloseSerial(void);

private:
    /*Function*/
    void initWidget();
    QStringList getAvailableComs();
    void updateControlState(bool connected);
    void addLog(QString log);
    void updateComNamesComboxView(QStringList tmpComs);

    /*Slot*/
    void slot_comOpen();
    void slot_comRecieve();
    void slot_comSend();
    void slot_updateComs();
//    void slot_hexRecieve(int state);
//    void slot_showTime(int state);
//    void slot_wordWrap(int state);
//    void slot_sendBack(int state);
//    void slot_rs485Mode(int state);
//    void slot_hexSend(int state);
//    void slot_autoSend(int state);

    enum ComSetIndex{
        ComName,
        BaudRate,
        DataBits,
        Parity,
        StopBits,
        FlowControl,
    };
//    struct{
//        bool showTime;
//        bool showHex;
//        bool sendHex;
//        bool sendBack;
//        bool rs485Mode;
//    }m_comSettings;

    /*Member*/
    int m_sendBack;
    QMap<ComSetIndex, QComboBox*> m_comSetControls;
    QTextEdit *m_textSend, *m_textRecv;
    QSerialPort *m_serialPort;
    QPushButton *m_btnOpen, *m_btnSend;
    QSpinBox *m_spinBoxSendMs;
    QComboBox *m_comboxComs;
    QMap<QString, QString> m_comsMap;
    QByteArray redata;
    //    QTimer *m_autoSendTimer;

    /********************************  Reboot Assist  ************************************/
public:
    bool RebootRunning;                     //正在进行重启标志位

public:
    void rebootSystem(QString KeyWord, QString UserName, QString PassWord); //进行一次重启动作
    int waitForString(QString str, int seccond);            //等待串口的某一行出现字符串,等待second秒
    int waitForStrings(QStringList strlist, int second);    //等待串口的某一行出现字符串列表,等待second秒
    void sendStr(QString);                                  //发送字符接口
    void sendStr(const char*);                              //发送字符接口(重构)

public slots:
    void slot_cleanLog();                                   //清理接收缓存
    void slot_saveLog(QString);                             //保存接收缓存到指定路径
};

#endif // SERIALPORT_H
